<?php
const URL_CURRENT = 'url://current';
const URL_RELOAD = 'url://reload';
const URL_BACK = 'url://back';

use think\exception\HttpResponseException;
use think\Response;
use think\response\Redirect;
use app\common\model\MallImage;
use think\db;

if (!function_exists('success')) {

    function success($msg = '操作成功', $url = URL_BACK, $data = '', $wait = 0, array $header = [])
    {
        result(1, $msg, $data, $url, $wait, $header);
    }
}


if (!function_exists('error')) {
    function error($msg = '操作失败', $url = URL_CURRENT, $data = '', $wait = 0, array $header = [])
    {
        result(0, $msg, $data, $url, $wait, $header);
    }
}

if (!function_exists('result')) {
    function result($code = 0, $msg = 'unknown', $data = '', $url = null, $wait = 3, array $header = [])
    {
        if (request()->isPost()) {
            $url = (strpos($url, '://') || 0 === strpos($url, '/')) ? $url : url($url);
            $result   = [
                'code' => $code,
                'msg'  => $msg,
                'data' => $data,
                'url'  => $url,
                'wait' => $wait,
            ];
            $response = Response::create($result, 'json')->header($header);
            throw new HttpResponseException($response);
        }


        if ($url === null) {
            $url = request()->server('HTTP_REFERER') ?? 'admin/index/index';
        }

        $response = new Redirect($url);

        $response->code(302)->params($data)->with([$code ? 'success_message' : 'error_message' => $msg, 'url' => $url]);

        throw new HttpResponseException($response);
    }
}

if(!function_exists('goods_imgs_upload')){
    /**
    * 简单对称加密算法之解密
    * @param String $union_id 指定id
    * @param String $type 类型
    * @param String $ori_img 原图
    * @param String $thumb_img 缩略图
    * @param String $ori_fm_img 原图封面
    * @param String $thumb_img_f 缩略图封面
    * @param String $goods_imgs 编辑时数据库中图片
    * @date 20190906
    * @return li
    */
    //上传商品图片
    function goods_imgs_upload($union_id,$type,$ori_img, $thumb_img,$ori_fm_img,$thumb_img_f,$goods_imgs=array())  
    {
        $_goodsimgsModel= new MallImage();
        //$tps = C('TMPL_PARSE_STRING');
        $inequality_img=array();
        if($ori_img){
            $ori_img_ys=array();
            if($goods_imgs){
                foreach ($goods_imgs as $key=> $v){
                    $ori_img_ys[$v['id']]=$v['ori_image'];
                    $goods_imgs[$v['id']]=$v;
                }
                $inequality_img = array_diff($ori_img_ys,$ori_img);//前端踢掉的图片
            }
            foreach ($ori_img as $key=> $v){
                if(in_array($v,$ori_img_ys,TRUE)){
                   if($v==$ori_fm_img && $thumb_img_f=$thumb_img[$key]){
                        $data3['is_cover']=1;
                    }else{
                        $data3['is_cover']=0;
                    }
                    $_goodsimgsModel->where(array('ori_image'=>$v))->update($data3);
                    continue;
                }
                $ori_img_url=trim($v);
                if(!$ori_img_url){
                    $array = array(
                        'code' =>400,
                        'msg' =>'上传文件处理失败',
                    );
                    return $array;
                }
                $thumb_img_url=trim($thumb_img[$key]);
                if(!$thumb_img_url){
                    $array = array(
                        'code' =>400,
                        'msg' =>'上传文件处理失败',
                    );
                    return $array;
                }
                $data1['ori_image']=$yu_url=$ori_img_url;
                $data1['thumb_image']=$slt_url=$thumb_img_url;
                if($v==$ori_fm_img && $thumb_img_f=$thumb_img[$key]){
                    $data1['is_cover']=1;
                }else{
                    $data1['is_cover']=0;
                }
                $data1['type']=$type;
                $data1['union_id']=$union_id;
                $_goodsimgsModel::create($data1);
            }
            if($inequality_img){
                foreach ($inequality_img as $key1=> $v1){
                    $mag=Db::table('yo_mall_image')->where('id',$key1)->delete();
                    if($mag){
                        $ori_image_del=Env::get('root_path').'public'.$goods_imgs[$key1]['ori_image'];
                        if(file_exists($ori_image_del))
                        {
                            unlink($ori_image_del);
                        }
                        $thumb_image_del=Env::get('root_path').'public'.$goods_imgs[$key1]['thumb_image'];
                        if(file_exists($thumb_image_del))
                        {
                            unlink($thumb_image_del);
                        }
                    }
                }
            }
        }
    }
}